using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Simulation.Framework
{
    public class AudioSingleManager : ManagerBehavior
    {
        private Dictionary<string, AudioExtensions> _audiosPool = new Dictionary<string, AudioExtensions>();
        private AudioExtensions _bgmAudioExtensions;
        private AudioExtensions _effectAudioExtensions;
        #region override

        public override void Init()
        {
            _audiosPool = new Dictionary<string, AudioExtensions>();
        }

        public override void Clear()
        {
            _audiosPool?.Clear();
            _audiosPool = null;

        }
        public void ClearAuio()
        {
            // if (bgmAudioExtensions != null) bgmAudioExtensions = null;
        }
        public override void Restar() { }

        #endregion

        #region BGM
        public void PlayBgm(string name, UnityAction onComplete = null)
        {
            UnityAction<AudioClip> addAudioExtensions = audio =>
            {
                if (audio == null)
                {
                    onComplete?.Invoke();
                    return;
                }

                if (_bgmAudioExtensions == null) _bgmAudioExtensions = new AudioExtensions(audio);
                _bgmAudioExtensions.PlayAudio(audio);
                _bgmAudioExtensions.type = AudioExtensions.AudioType.BGM;
                _bgmAudioExtensions.onComplete = onComplete;
                
            };

            SingleBehavior<ResManagerByNet>.getInstance().LoadAudio(name, clip => {
                addAudioExtensions(clip);
            });
        }

        public void PlayBGMVolume(float volue)
        {
            if (_bgmAudioExtensions == null) return;
            _bgmAudioExtensions.PlayVolume(volue);
        }

        #endregion

        #region EFFECT
        public void PlayEffect(string name, UnityAction onComplete = null, UnityAction<float> audioLength = null)
        {
            // StopEffect();
            UnityAction<AudioClip> addAudioExtensions = audio =>
            {
                if (audio == null)
                {
                    OnComplete();
                    return;
                }

                if (_effectAudioExtensions == null) _effectAudioExtensions = new AudioExtensions(audio);
                _effectAudioExtensions.PlayAudio(audio);
                _effectAudioExtensions.type = AudioExtensions.AudioType.EFFECT;
                _effectAudioExtensions.onComplete = OnComplete;
                audioLength?.Invoke(audio.length);// audioLength(audio.length);
            };

            SingleBehavior<ResManagerByNet>.getInstance().LoadAudio(name, clip => {
                addAudioExtensions(clip);
            });

            //------------


            void OnComplete()
            {

                onComplete?.Invoke();
            }
        }
        #endregion

        #region STOP
        public void StopEffect()
        {
            if (_effectAudioExtensions != null) { _effectAudioExtensions.Stop(); ResetAudio(); }
        }
        public void ResetAudio()
        {

            _bgmAudioExtensions = null;
            _effectAudioExtensions = null;
        }
        #endregion
    }
}